const dbConfig = require("./index")
const dayjs = require('dayjs')
const { Parameters } = require('../utils/utils')

/**
 * 添加一条用户信息
*/
const addUsersInfo = (req, res) => {
  const { phoneNumber } = req.body
  dbConfig.sqlConnect(`select * from tool.usersInfo where phoneNumber = ?`, [phoneNumber], (data, err) => {
    if (err) return res.send({ code: 400, msg: '错误，请稍后重试' })
    if (data?.length) {
      res.send({
        code: 201,
        data: data[0],
        msg: '该手机号已登记,请勿重复操作'
      })
    } else {
      const { keys, values, num } = new Parameters(req.body).add({ shareUserId: req?.shareUserId || 1, createTime: dayjs().format('YYYY-MM-DD') })
      const sql = `insert into tool.usersInfo(${keys}) values(${num})`
      dbConfig.sqlConnect(sql, [...values], (data, err) => {
        if (err) return res.sends(err)
        res.sends('成功')
      }, res)
    }

  }, res)

}

/**
 * 获取用户信息列表
*/
const getUsersInfoList = (req, res) => {
  const { current, size, createTime, name, phone } = req.query

  let sql_1 = 'select count(*) as total from tool.usersInfo'
  let sqlArr_1 = []

  let sql_2 = `select * from tool.usersInfo`
  let sqlArr_2 = []

  if (createTime) {
    let startTime = '';
    let endTime = ''
    if (Array.isArray(createTime)) {
      const [start, ent] = createTime
      startTime = dayjs(start).format('YYYY-MM-DD') + ' 00:00:00'
      endTime = dayjs(ent).format('YYYY-MM-DD') + ' 23:59:59'
    } else {

      switch (createTime) {
        case 'toDay':
          startTime = dayjs().format('YYYY-MM-DD') + ' 00:00:00'
          endTime = dayjs().format('YYYY-MM-DD') + ' 23:59:59'
          break;

        case 'lastDay':
          startTime = dayjs().subtract(1, 'day').format('YYYY-MM-DD') + ' 00:00:00'
          endTime = dayjs().subtract(1, 'day').format('YYYY-MM-DD') + ' 23:59:59'
          break;

        case 'toWeek':
          const weekdayNumber = dayjs().day();
          if (weekdayNumber == 1) {
            startTime = dayjs().format('YYYY-MM-DD') + ' 00:00:00'
          } else if (weekdayNumber == 0) {
            startTime = dayjs().subtract(6, 'day').format('YYYY-MM-DD') + ' 00:00:00'
          } else {
            startTime = dayjs().subtract(weekdayNumber - 1, 'day').format('YYYY-MM-DD') + ' 00:00:00'
          }
          endTime = dayjs().format('YYYY-MM-DD') + ' 23:59:59'
          break;

        case 'toMonth':
          startTime = dayjs().startOf('month').format('YYYY-MM-DD') + ' 00:00:00'
          endTime = dayjs().format('YYYY-MM-DD') + ' 23:59:59'
          break;

        default:
          break;
      }
    }
    const addSqlContent = ` where createTime between '${startTime}' and '${endTime}'`
    if (createTime !== 'all') {
      sql_1 += addSqlContent
      sql_2 += addSqlContent
    }
  }
  if (name) {
    if (sql_1.includes('where')) {
      sql_1 += ` and name = '${name}'`
      sql_2 += ` and name = '${name}'`
    } else {
      sql_1 += ` where name = '${name}'`
      sql_2 += ` where name = '${name}'`
    }
  }
  if (phone) {
    if (sql_1.includes('where')) {
      sql_1 += ` and phoneNumber = '${phone}'`
      sql_2 += ` and phoneNumber = '${phone}'`
    } else {
      sql_1 += ` where phoneNumber = '${phone}'`
      sql_2 += ` where phoneNumber = '${phone}'`
    }
  }
  console.log(sql_2, '=====');
  dbConfig.sqlConnect(sql_1, sqlArr_1, (nums, err) => {
    console.log(nums);
    const total = nums[0].total
    sql_2 += ` order by id limit ${size} offset ${(current - 1) * size}`
    dbConfig.sqlConnect(sql_2, sqlArr_2, (data, err) => {
      if (err) return res.sends('参数错误')
      res.sends({
        data,
        pagination: { total },

      })
    }, res)
  }, res)



}


module.exports = { addUsersInfo, getUsersInfoList }
